<template>
    <el-dialog
        :title="title"
        append-to-body
        destroy-on-close
        :model-value="showDialog"
        @close="closeDialog()"
    >
        <!-- 表单 -->
        <el-form
            :model="form"
            ref="formRef"
            :rules="rules"
            :inline="false"
            label-width="120px"
        >
          <!--            <el-form-item label="仓库url" prop="url">-->
          <!--                <el-input v-model="url" placeholder="" clearable></el-input>-->
          <!--            </el-form-item>-->
          <foreach collection="columnList" index="index" item="column" open="" close="" separator="">
              <el-form-item label="#{column.comment}" prop="#{column.name}" show-overflow-tooltip>
                  <el-input v-model="#{column.name}" placeholder="" clearable></el-input>
              </el-form-item>
          </foreach>
        </el-form>

        <template v-slot:footer>
            <span>
                <el-button @click="closeDialog()">取消</el-button>
                <el-button type="primary" @click="onSubmit()">确定</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script setup>
import { reactive, ref, toRefs, defineProps, defineEmits } from "vue";

const props = defineProps({
    showDialog: {
        type: Boolean,
        default: true,
    },
    title: {
        type: String,
        default: "添加",
    },
    rowData: {
        type: Object,
        default: null,
    },
});
const emit = defineEmits(["closeDialog"]);
const { title, rowData } = toRefs(props);
const closeDialog = () => {
    emit("closeDialog", false);
};

// const form = reactive({
//   url: "",
//   username: "",
//   password: "",
//   isDefault: false,
// });
const form = reactive({
  <foreach collection="columnList" index="index" item="column" open="" close="" separator=",">
    #{column.name}:""
  </foreach>
});
// const { url, username, password, isDefault } = toRefs(form);
const {
  <foreach collection="columnList" index="index" item="column" open="" close="" separator=",">
    #{column.name}
  </foreach>
} = toRefs(form);
const formRef = ref(null);

// const rules = {
// url: [
//     {
//         required: true,
//         message: "请输入仓库url",
//         trigger: "blur",
//     },
// ],
//  }
const rules = {

  <foreach collection="columnList" index="index" item="column" open="" close="" separator=",">
    #{column.name}: [
        {
            required: true,
            message: "请输入#{column.comment}",
            trigger: "blur",
        },
    ]
  </foreach>

};

/**
 * @description: 数据初始化
 * @param {*}
 * @return {*}
 * rowData.value &&
 *     (
 *         (url.value = rowData.value.url),
 *     (username.value = rowData.value.username),
 *     (password.value = rowData.value.password),
 *     (isDefault.value = rowData.value.isDefault)
 *     );
 */
rowData.value &&
(
    <foreach collection="columnList" index="index" item="column" open="" close="" separator=",">
      (#{column.name}.value=rowData.value.#{column.name})
    </foreach>
);
/**
 * @description:提交
 * @param {*}
 * @return {*}
 */
const onSubmit = () => {
  formRef.value.validate(async (valid) => {
        if (valid) {
            let res;
            if (title.value === "添加") {
                res = await VE_API.#{Module}Api.#{module}Story(form);
            } else {
                res = await VE_API.#{Module}Api.#{module}Story({
                    id: rowData.value.id,
                    ...form,
                });
            }
            const { code } = res;
            if (code === 0) {
                closeDialog();
            }
        } else {
            console.log("error submit!!");
            return false;
        }
    });
};
</script>

<style lang="scss">
.fl {
    float: left;
}
.p0 {
    padding: 0 !important;
}
.role_edit_item {
    width: 100%;
}
</style>
